# vim: ts=4:sw=4:syntax=perl

use gri_report_config;
use gri_userprefs;
use Data::Dumper;

{
my @mon=qw(
	January February March April May June
	July August September October November December
);

sub mm_recent_files {

	my $count=5;
	my $age=72;
	my $user_stuff=get_cookie_value(name=>"gri_dbm_intranet");
	my @F=split(/\s+/,$user_stuff);
	my $user_name=$F[0];

	my $show_pane=1;
	# See if we have preferemces
	my $users_dir=$::GRI_FRONTEND->get_global_directory("users");
	if(-f "$users_dir/$user_name-prefs.xml") {
		my $xx=new gri_userprefs("$users_dir/$user_name-prefs.xml");
		if($xx) {
			my ($desc,$val)=$xx->get_pref_value("recent_files_pane");
			my ($desc2,$val2)=$xx->get_pref_value("recent_files_pane_count");
			my ($desc3,$val3)=$xx->get_pref_value("recent_files_oldest");
			if(defined($val) && ($val eq "0" || $val eq "false" || $val eq "no")) {
				$show_pane=0;
			}
			if(defined($val2) && $val2 =~ /^\d+$/ && int($val2)>=1) {
				$count=int($val2);
			}
			if(defined($val3) && $val3 =~ /^\d+$/ && int($val3)>=1) {
				$age=int($val3);
			}
		}
	} elsif(-f "$users_dir/__DEFAULT__-prefs.xml") {
		my $xx=new gri_userprefs("$users_dir/__DEFAULT__-prefs.xml");
		if($xx) {
			my ($desc,$val)=$xx->get_pref_value("recent_files_pane");
			if(defined($val) && ($val eq "0" || $val eq "false" || $val eq "no")) {
				$show_pane=0;
			}
		}
	}

	return "" if $show_pane==0;

	#################################################################
	# If we the user preference or default is 1, so scan list of	#
	# recent files that are applicable to the user and process...	#
	#################################################################

	my $lref=$::GRI_FRONTEND->get_portal_list();
	my @L=(@$lref);
	my $tmpdir=$::GRI_FRONTEND->get_tmp_content_dir();
	return "" if ! -f "$tmpdir/report_updates.lst";

	my @RL=();
	for my $c (@L) {
		my $a=$::GRI_FRONTEND->is_user_allowed($c,$user_name);
		next if !$a;
		my $portal_fname=$::GRI_FRONTEND->get_portal_filename($c);
		my ($lref,$err)=$::GRI_FRONTEND->get_portal_report_names($c);
		if($lref) {
			for my $rep (@$lref) {
				push @RL,[$rep,$c];
			}
		}
	}
	my $fd;
	if(!open($fd,"$tmpdir/report_updates.lst")) {
		return "";
	}
	my @D=<$fd>;
	close($fd);
	my @sb=stat("$tmpdir/report_updates.lst");
	my $tstr=$::GRI_FRONTEND->long_datetime_str($sb[9]);
	my $sss="[Last Updated $tstr]";
	my $ctime=time-($age*3600);
	my @RL2=();
	for my $_rep (@RL) {
		$rep=$_rep->[0];
		$portal=$_rep->[1];
		my @LL=grep {/^$rep\|/} @D;
		if(@LL) {
			my @LL2=split(/\|/,$LL[0]);
			if($LL2[3]>$ctime) {
				my $x="$LL[0]|$portal";
				$x =~ s/\n//g;
				push @RL2,$x;
			}
		}
	}
	@RL2=sort {
		my @A=split(/\|/,$a);
		my @B=split(/\|/,$b);
		$A[3] <=> $B[3];
	} @RL2;
	my $str="";
	# Sort and return most $count most recent... 
	my $row=0;
	while($count) {
		$count--;
		my $x2=pop @RL2;
		last if !defined($x2);
		my @x=split(/\|/,$x2);
		if(!length($str)) {
			$str="<br><font class=stdtexttitle>Latest Available Reports $sss</font><br>
<table class=gri_container cols=5>
  <tr class=gri_headerrow>
    <td class=gri_cell>Name</td>
    <td class=gri_cell>Type</td>
    <td class=gri_cell>Date/Time</td>
    <td class=gri_cell-right>Size</td>
    <td class=gri_cell-right>Actions</td>
</tr>\n";
		}
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		$row++;
		$str.="<tr id=$class><td class=gri_cell>$x[6]</td>";
		$str.="<td class=gri_cell>$x[5]</td>";
		my $size;
		if($x[2]==0) {
			$size="Empty";
		} elsif($x[2]<10000) {
			$size="$x[2] Bytes";
		} else {
			$size=sprintf("%d Kb",$x[2]/1024);
		}
		my $tref=$::GRI_FRONTEND->internal_to_posix_time($x[4]);
		$tstr=$::GRI_FRONTEND->datetime_str($tref);
		$str.="<td class=gri_cell>$tstr</td>";
		$str.="<td class=gri_cell-right>$size</td>";
		my $links="";
		my $n2=string_to_hex($x[0]);
		my $n3=string_to_hex($x[3]);
		my $n4=string_to_hex($x[8]);
		my $l2=link_to_str("stdlink","View","portal_file_view","portal=$n4,repname=$n2,repmtime=$n3");
		$str.="<td class=gri_cell-right>$l2</td>";
		$str.="</tr>\n";
	}
	$str.="</table>" if length($str);
	return $str;
}

}

1;

